#ifndef _CZ_UTIL_H_
#define _CZ_UTIL_H_

#include <stdlib.h>
#include <assert.h>

#ifdef _MSC_VER
inline FILE* fopen_local(char* filename, char* mode)
{
  FILE* fp;
  if(fopen_s(&fp, filename, mode) != 0)
    return 0; // failed!
  return fp;
}
#if _MSC_VER > 1300
#undef fopen
#define fopen fopen_local
#endif
#endif

inline char* file2mem(char* filename, unsigned int* memsz)
{
  assert(filename && memsz);

  FILE* fp = fopen(filename, "rb");
  if(!fp)
    return 0; // error!

  fseek(fp, 0, SEEK_END);
  int fsz = ftell(fp);
  char* buffer = (char*)malloc(fsz+2);
  fseek(fp, 0, SEEK_SET);
  size_t bsz = fread(buffer, sizeof(char), fsz, fp);
  buffer[fsz]=buffer[fsz+1]=0;
  fclose(fp);

  *memsz = bsz;
  return buffer;
}

inline void freemem(char* buf) { free(buf); }

#endif  // _CZ_UTIL_H_
